/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elicitor.core;

import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 *
 * @author dileepa
 */
public class PraatCall {

    String fileLocation;
    
    public PraatCall(String fileLocation) {
        
        this.fileLocation = fileLocation;
    
    }
    
    
    public void createFile(){
        PrintStream out = null;
        
try {
    out = new PrintStream(new FileOutputStream("voiceAtt.praat"));
    out.println("Read from file... "+fileLocation);
    out.println("original= selected(\"Sound\")");
    out.println("orgMinimumSoundPressure=Get minimum... 0 0 Sinc70");
    out.println("orgMaximumSoundPressure=Get maximum... 0 0 Sinc70");
    out.println("orgMean=Get mean... 0 0 0");
    out.println("orgRootMeanSquare=Get root-mean-square... 0 0");
    out.println("orgStd=Get standard deviation... 0 0 0");
    out.println("orgEnergy=Get energy... 0 0");
    out.println("orgPower=Get power... 0 0");
    out.println("orgEnergyInAir=Get energy in air");
    out.println("orgPowerInAir=Get power in air");
    out.println("orgIntensity=Get intensity (dB)");
    out.println("pitch=To Pitch... 0 5 600");
    out.println("pitchMinimum=Get minimum... 0 0 Hertz Parabolic");
    out.println("pitchMaximum=Get maximum... 0 0 Hertz Parabolic");
    out.println("pitchQ1=Get quantile... 0 0 0.25 Hertz");
    out.println("pitchQ2=Get quantile... 0 0 0.5 Hertz");
    out.println("pitchQ3=Get quantile... 0 0 0.75 Hertz");
    out.println("pitchMean=Get mean... 0 0 Hertz");
    out.println("pitchStd=Get standard deviation... 0 0 Hertz");
    out.println("echo \'orgMinimumSoundPressure\',\'orgMaximumSoundPressure\',\'orgMean\',\'orgRootMeanSquare\',\'orgStd\',\'orgEnergy\',\'orgPower\',\'orgEnergyInAir\',\'orgPowerInAir\',\'orgIntensity\',\'pitchMinimum\',\'pitchMaximum\',\'pitchQ1\',\'pitchQ2\',\'pitchQ3\',\'pitchMean\',\'pitchStd\'");
}catch(Exception e){
    System.err.println("Errroer "+e.getMessage());
}
finally {
    if (out != null) out.close();
}

    }
    
 public void createNewVoiceFile(){
        PrintStream out = null;
        
try {
    out = new PrintStream(new FileOutputStream("voiceAtt.praat"));
    out.println("silencedb = -25");
    out.println("mindip = 2");
    out.println("showtext = 1");
    out.println("minpause = 0.3");
    out.println("Read from file... "+fileLocation);
    out.println("soundname$ = selected$(\"Sound\")");
    out.println("soundid = selected(\"Sound\")");
    out.println("originaldur = Get total duration");
    out.println("bt = Get starting time");
    out.println("To Intensity... 50 0 yes");
    out.println("intid = selected(\"Intensity\")");
    out.println("start = Get time from frame number... 1");
    out.println("nframes = Get number of frames");
    out.println("end = Get time from frame number... 'nframes'");
    out.println("minint = Get minimum... 0 0 Parabolic");
    out.println("maxint = Get maximum... 0 0 Parabolic");
    out.println("max99int = Get quantile... 0 0 0.99");
    out.println("threshold = max99int + silencedb");
    out.println("threshold2 = maxint - max99int");
    out.println("threshold3 = silencedb - threshold2");
    out.println("if threshold < minint");
    out.println("threshold = minint");
    out.println("endif");
    out.println("To TextGrid (silences)... threshold3 minpause 0.1 silent sounding");
    out.println("textgridid = selected(\"TextGrid\")");
    out.println("silencetierid = Extract tier... 1");
    out.println("silencetableid = Down to TableOfReal... sounding");
    out.println("nsounding = Get number of rows");
    out.println("npauses = 'nsounding'");
    out.println("speakingtot = 0");
    out.println("for ipause from 1 to npauses");
    out.println("beginsound = Get value... 'ipause' 1");
    out.println("endsound = Get value... 'ipause' 2");
    out.println("speakingdur = 'endsound' - 'beginsound'");
    out.println("speakingtot = 'speakingdur' + 'speakingtot'");
    out.println("endfor");
    out.println("select 'intid'");
    out.println("Down to Matrix");
    out.println("matid = selected(\"Matrix\")");
    out.println("To Sound (slice)... 1");
    out.println("sndintid = selected(\"Sound\")");
    out.println("intdur = Get total duration");
    out.println("intmax = Get maximum... 0 0 Parabolic");
    out.println("To PointProcess (extrema)... Left yes no Sinc70");
    out.println("ppid = selected(\"PointProcess\")");
    out.println("numpeaks = Get number of points");
    out.println("for i from 1 to numpeaks");
    out.println("t'i' = Get time from index... 'i'");
    out.println("endfor");
    out.println("select 'sndintid'");
    out.println("peakcount = 0");
    out.println("for i from 1 to numpeaks");
    out.println("value = Get value at time... t'i' Cubic");
    out.println("if value > threshold");
    out.println("peakcount += 1");
    out.println("int'peakcount' = value");
    out.println("timepeaks'peakcount' = t'i'");
    out.println("endif");
    out.println("endfor");
    out.println("select 'intid'");
    out.println("validpeakcount = 0");
    out.println("currenttime = timepeaks1");
    out.println("currentint = int1");
    out.println("for p to peakcount-1");
    out.println("following = p + 1");
    out.println("followingtime = timepeaks'following'");
    out.println("dip = Get minimum... 'currenttime' 'followingtime' None");
    out.println("diffint = abs(currentint - dip)");
    out.println("if diffint > mindip");
    out.println("validpeakcount += 1");
    out.println("validtime'validpeakcount' = timepeaks'p'");
    out.println("endif");
    out.println("currenttime = timepeaks'following'");
    out.println("currentint = Get value at time... timepeaks'following' Cubic");
    out.println("endfor");
    out.println("select 'soundid'");
    out.println("To Pitch (ac)... 0.02 30 4 no 0.03 0.25 0.01 0.35 0.25 450");
    out.println("pitchid = selected(\"Pitch\")");
    out.println("voicedcount = 0");
    out.println("for i from 1 to validpeakcount");
    out.println("querytime = validtime'i'");
    out.println("select 'textgridid'");
    out.println("whichinterval = Get interval at time... 1 'querytime'");
    out.println("whichlabel$ = Get label of interval... 1 'whichinterval'");
    out.println("select 'pitchid'");
    out.println("value = Get value at time... 'querytime' Hertz Linear");
    out.println("if value <> undefined");
    out.println("if whichlabel$ = \"sounding\"");
    out.println("voicedcount = voicedcount + 1");
    out.println("voicedpeak'voicedcount' = validtime'i'");
    out.println("endif");
    out.println("endif");
    out.println("endfor");
    out.println("timecorrection = originaldur/intdur");
    out.println("if showtext > 0");
    out.println("select 'textgridid'");
    out.println("Insert point tier... 1 syllables");
    out.println("for i from 1 to voicedcount");
    out.println("position = voicedpeak'i' * timecorrection");
    out.println("Insert point... 1 position 'i'");
    out.println("endfor");
    out.println("endif");
    out.println("Read from file... "+fileLocation);
    out.println("snd = selected(\"Sound\")");
    out.println("pitch=To Pitch... 0 5 600");
    out.println("pitchMinimum=Get minimum... 0 0 Hertz Parabolic");
    out.println("pitchMaximum=Get maximum... 0 0 Hertz Parabolic");
    out.println("pitchQ1=Get quantile... 0 0 0.25 Hertz");
    out.println("pitchQ2=Get quantile... 0 0 0.5 Hertz");
    out.println("pitchQ3=Get quantile... 0 0 0.75 Hertz");
    out.println("pitchMean=Get mean... 0 0 Hertz");
    out.println("pitchStd=Get standard deviation... 0 0 Hertz");
    out.println("pit = selected (\"Pitch\")");
    out.println("select snd");
    out.println("plus pit");
    out.println("To PointProcess (cc)");
    out.println("jitter = Get jitter (ddp)... 0 0 0.0001 0.02 1.3");
    out.println("plus snd");
    out.println("shimmer = Get shimmer (dda)... 0 0 0.0001 0.02 1.3 1.6");
    out.println("Read from file... "+fileLocation);
    out.println("snd = selected(\"Sound\")");
    out.println("To Formant (burg)... 0 3 3500 0.025 50");
    out.println("meanf1 = Get mean... 1 0 0 Hertz");
    out.println("meanf2 = Get mean... 2 0 0 Hertz");
    out.println("meanf3 = Get mean... 3 0 0 Hertz");
    out.println("medianf1 = Get quantile... 1 0 0 Hertz 0.5");
    out.println("medianf2 = Get quantile... 2 0 0 Hertz 0.5");
    out.println("medianf3 = Get quantile... 3 0 0 Hertz 0.5");
    out.println("stdevf1 = Get standard deviation... 1 0 0 Hertz");
    out.println("stdevf2 = Get standard deviation... 2 0 0 Hertz");
    out.println("stdevf3 = Get standard deviation... 3 0 0 Hertz");
    out.println("Read from file... "+fileLocation);
    out.println("snd = selected(\"Sound\")");
    out.println("To Intensity... 70 0 yes");
    out.println("meanspl = Get mean... 0 0 energy");
    out.println("medianspl = Get quantile... 0 0 0.5");
    out.println("stdspl = Get standard deviation... 0 0");
    out.println("minspl = Get minimum... 0 0 Parabolic");
    out.println("maxspl = Get maximum... 0 0 Parabolic");
    out.println("select 'intid'");
    out.println("plus 'matid'");
    out.println("plus 'sndintid'");
    out.println("plus 'ppid'");
    out.println("plus 'pitchid'");
    out.println("plus 'silencetierid'");
    out.println("plus 'silencetableid'");
    out.println("Remove");
    out.println("if showtext < 1");
    out.println("select 'soundid'");
    out.println("plus 'textgridid'");
    out.println("Remove");
    out.println("endif");
    out.println("speakingrate = 'voicedcount'/'originaldur'");
    out.println("articulationrate = 'voicedcount'/'speakingtot'");
    out.println("npause = 'npauses'-1");
    out.println("asd = 'speakingtot'/'voicedcount'");
    out.println("y1 = 'meanf2'*'maxspl'*'stdspl'");
    out.println("y2 = 'pitchStd'*'stdspl'");
    out.println("y3 = 'stdevf2'*'medianf3'");
    out.println("y4 = 'pitchQ3'*'stdspl'");
    out.println("y5 = 'medianf3'*'stdspl'");
    out.println("echo 'articulationrate','pitchMinimum','pitchMaximum','pitchQ1','pitchQ2','pitchQ3','pitchMean','pitchStd','jitter','shimmer','meanf1','meanf2','meanf3','medianf1','medianf2','medianf3','stdevf1','stdevf2','stdevf3','meanspl','medianspl','stdspl','minspl','maxspl','y1','y2','y3','y4','y5'");
//    out.println("echo 'y1','y2','y3','y4','y5'");
}catch(Exception e){
    System.err.println("Errroer "+e.getMessage());
}
finally {
    if (out != null) out.close();
}

    }
    
    
    
    
    
   public void sendPraat(){
              Runtime run = Runtime.getRuntime();
        try {
            String[] sendpraatCom = new String[3];
            sendpraatCom[0] = "./sendpraat";
            sendpraatCom[1] = "praat";
            sendpraatCom[2] = "execute " + "/home/dileepa/Desktop/FYP_Elicitor/elicitor/voiceAtt.praat";
            run.exec(sendpraatCom);
        } catch (Exception ex) {
            System.err.println("Error... "+ex.getMessage());
        }
   } 
    
    
    
    
}
